#include "EbnCMEnergyHistFromGoal.h"

CEbnCMEnergyHistFromGoal::CEbnCMEnergyHistFromGoal(CEbnGoal * goal)
{
	mpGoal = goal;
}

CEbnCMEnergyHistFromGoal::~CEbnCMEnergyHistFromGoal(void)
{
    vector<CEbnCMEnergyHist *>::iterator itHist;

    for (itHist = mCMEnergyHists.begin(); itHist != mCMEnergyHists.end(); itHist++)
    {
        CEbnCMEnergyHist * pCMEnergyHist = (* itHist);
        delete pCMEnergyHist;
    }

    mCMEnergyHists.clear();
}

void CEbnCMEnergyHistFromGoal::addCMEnergyHist(CEbnCompetenceModule * cm)
{
    CEbnCMEnergyHist * pCMEnergyHist = new CEbnCMEnergyHist(cm);
    mCMEnergyHists.push_back(pCMEnergyHist);
}

CEbnCMEnergyHist * CEbnCMEnergyHistFromGoal::getCMEnergyHist(CEbnCompetenceModule * cm)
{
    vector<CEbnCMEnergyHist *>::iterator itHist;

    for (itHist = mCMEnergyHists.begin(); itHist != mCMEnergyHists.end(); itHist++)
    {
        if ((* itHist)->isHostCompetenceModule(cm))
        {
            return (* itHist);
        }
    }

    return NULL;
}

float CEbnCMEnergyHistFromGoal::getEnergyHist(CEbnCompetenceModule * cm, int time)
{
    CEbnCMEnergyHist * pHist = getCMEnergyHist(cm);

    if (NULL == pHist)
    {
        return 0;
    }

    return pHist->getEnergyHist(time);
}

bool CEbnCMEnergyHistFromGoal::saveToEnergyHist(CEbnCompetenceModule * cm, float activation)
{
    CEbnCMEnergyHist * pHist = getCMEnergyHist(cm);

    if (NULL == pHist)
    {
        return false;
    }

    pHist->addEnergyHist(activation);

    return true;
}

bool CEbnCMEnergyHistFromGoal::isHostGoal(CEbnGoal * goal)
{
    if (mpGoal == goal)
    {
        return true;
    }

    return false;
}
